﻿<?php
/**
* Store Categories Management
*/
class Admin_StorecategoriesController extends Zend_Controller_Action{
		
	function init(){
		// System_Template::using('blank','Admin');
		System_Template::using('globalconfig','Admin');
		
		// Lấy các parameter trong request URL
		$this->Params = $this->getRequest()->getParams();            
		$this->view->maintitle = "Store Categories";
		$this->model = new Admin_Model_StoreCategories();
		$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/log.txt');
		$this->logger = new Zend_Log($writer);
		parent::init();
	}
	
	/**
    * List Store Categories
    */
	public function indexAction()
    {	
		$parameter = array();
		$parameter['PARENTID'] = 0;
		$data = $this->model->SelectAll($parameter);
		$this->view->data = $data;
    }
	
	/**
    * INVISIBLE
	* Collapse an Category
    */
	public function expandAction()
    {	
		$parameter = array();
		$parameter['PARENTID'] = $this->Params['itemID'];
		
		$this->_helper->layout->disableLayout();
		
		$numberOfItem = $this->model->count($parameter); 
		$data = $this->model->SelectAll($parameter);
		$this->view->data = $data;
		$this->view->level = $this->Params['level'];
		$this->view->logger = $this->logger;
		$this->view->parentRow = $this->Params['itemID'];
		/* for($i=1;$i<10000000; $i++); */
    }
	
	/**
    * INVISIBLE
	* Redirect to add or edit form
    */
    function inputAction(){
        $id = (isset($this->Params['id'])?$this->Params['id']:0);
		
		 /* Trường hợp cập nhật */
		if($id>0){
			$data = $this->model->find($id)->current();
			$this->view->data = $data;
			$this->view->id = $id;
			$parentID = $data->PARENTID;
		} else {
			$parentID = (isset($this->Params['parentid'])?$this->Params['parentid']:0);
		}
        $hierachy = array();
			
		$currentParentID = $parentID;
		$i=0;
		
		/* Lấy tên các cấp thư mục cha để lưu vô 1 array */
		while($currentParentID!=0){
			$currentParent = $this->model->find($currentParentID)->current();
			$hierachy[$i] = $currentParent->NAME;
			$currentParentID = $currentParent->PARENTID;
			$i++;
		}
		
		/* Lưu tên của các cấp thư mục cha để tiện hiển thị */
		$this->view->hierachy = $hierachy;
		$this->view->parentID = $parentID;
    }
	
	/**
    * INVISIBLE
	* Save a new category or edit an existed category
    */
	function saveAction(){
		if($this->Params['STATUS']=='on'){
			$this -> Params ['STATUS'] = '1';
		} else {
			$this -> Params ['STATUS'] = '0';
		}
		if($this->Params['STORECATEGORYID']==''){
			$this->model->addNewCategory($this->Params);
		} else {
			$this->model->updateCategory($this->Params);
		}
        $this->_redirect('/admin/storecategories/index');
    }
	
	/**
    * INVISIBLE
	* Change status of a category
    */
    function changestatusAction() {
        $db = $this-> model -> getDefaultAdapter();
        $itemID = $this->Params['itemID'];
		$status = $this->Params['status'];
		// $this->logger->info($itemID);
		echo $itemID;
		$this->model->changeStatusItem($itemID,$status);
        exit;
    }
	
	/**
    * INVISIBLE
	* Change status of selected categories
    */
	function changestatusmultiitemsAction(){
		$ids = $this -> Params['itemIDs'];
		$status = $this -> Params['status'];
        $db = $this -> model -> getDefaultAdapter();
		
        foreach($ids as $t){
            echo ','.$t;
			if($t!=''){
				$this->model->changeStatusItem($t,$status);
			}
        }
		exit;
	}
	
	/**
    * INVISIBLE
	* Delete a category
    */
	
	function deleteAction() {
        $db = $this-> model -> getDefaultAdapter();
        $itemID = $this->Params['itemID'];
		// $this->logger->info($itemID);
		echo $itemID;
		$this->model->deleteItem($itemID);
        exit;
    }
	
	/**
    * INVISIBLE
	* Delete selected categories
    */
	function deletemultiitemAction(){
		$ids = $this -> Params['itemIDs'];
        $db = $this -> model -> getDefaultAdapter();
		
        foreach($ids as $t){
            echo ','.$t;
			if($t!=''){
				$this->model->deleteItem($t);
			}
        }
		exit;
	}
}